﻿using Microsoft.AspNetCore.Mvc;
using NLog;
using OddUserManager.Model;
using OddUserManager.Service.IServices;
using OddUserManager.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace OddUserManager.Web.Controllers
{
    public class MenusController : BaseController
    {
        static Logger log = LogManager.GetCurrentClassLogger();
        IMenusService _menusService;
        public MenusController(IMenusService menusService) {
            _menusService = menusService;
        }
        // GET: Menus
        public IActionResult Index()
        {
            return View();
        }
        public JsonResult GetAllList()
        {
            JsonData data = new JsonData();
            data.Data = _menusService.GetAllList();
            return Json(data);
        }

       

        [HttpGet]
        public JsonResult GetMenusById(int id)
        {
            JsonData data = new JsonData();
            data.Data = _menusService.GetMenusById(id);
            return Json(data);
        }


        
        /// <summary>
        /// 获取功能树JSON数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuTreeData()
        {
            var menus = _menusService.GetAllList().OrderBy(p=>p.SerialNumber).ToList();
            List<TreeModel> treeModels = new List<TreeModel>();
            foreach (var menu in menus)
            {
                treeModels.Add(new TreeModel() { Id = menu.Id.ToString(), Text = menu.Name, Parent = menu.ParentId == 0 ? "#" : menu.ParentId.ToString() });
            }
            JsonData data = new JsonData();
            data.Data = treeModels;
            return Json(data);
        }
        /// <summary>
        /// 获取子级功能列表
        /// </summary>
        /// <returns></returns>
        public IActionResult GetMenusByParent(int parentId, int startPage, int pageSize)
        {
            int rowCount = 0;
            var result = _menusService.GetMenusByParent(parentId, startPage, pageSize, out rowCount);

            JsonData data = new JsonData();
            data.Data=new PageCollection
            {
                PageIndex =startPage,
                PageSize =pageSize,
                AllCount = rowCount,
                PageCount = Math.Ceiling(Convert.ToDecimal(rowCount) / pageSize),
                Data = result
            };

            return Json(data);
        }
        /// <summary>
        /// 新增或编辑功能
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public IActionResult Edit(Menus dto)
        {
            JsonData data = new JsonData();
            
            if (_menusService.InsertOrUpdateMenus(dto))
            {
                data.Message = "更新成功";
            }
            return Json(data);
        }

        public IActionResult DeleteMuti(string ids)
        {
            JsonData data = new JsonData();
            try
            {
                string[] idArray = ids.Split(',');
                List<int> delIds = new List<int>();
                foreach (string id in idArray)
                {
                    delIds.Add(int.Parse(id));
                }
                _menusService.DeleteBatchMenus(delIds);
                data.Message = "删除完成";
            }
            catch (Exception ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
            return Json(data);
        }
        public IActionResult Delete(int id)
        {
            JsonData data = new JsonData();
            try
            {
                _menusService.DeleteMenusById(id);
                data.Message = "删除完成";
            }
            catch (Exception ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
            return Json(data);
        }
    }
}